package com.gitee.zhanyao.single.system.service.impl;

import com.gitee.zhanyao.single.system.service.SysIconService;
import com.gitee.zhanyao.single.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.File;
import java.io.IOException;

@Slf4j
@Transactional
@Service
public class SysIconServiceImpl implements SysIconService {

    @Override
    public void generateFontawesome() {
        try {
            getSvgFiles("static/plugins/fontawesome-free/svgs/brands", "fab");
            getSvgFiles("static/plugins/fontawesome-free/svgs/regular", "far");
            getSvgFiles("static/plugins/fontawesome-free/svgs/solid", "fas");
        } catch (IOException e) {
            log.error(ExceptionUtils.getStackTrace(e));
        }
    }

    private void getSvgFiles(String fileFullPath, String prefix) throws IOException {
        Resource resource = FileUtil.resourceLoader(fileFullPath);
        File file = resource.getFile();
        File[] files = file.listFiles();
        String filename;
        for (File svgFile : files) {
            filename = svgFile.getName();
            log.info(prefix + " fa-" + filename.substring(0, filename.indexOf(".")));
        }
    }

}
